vignettes for dave/FLUXSYNTHU_VIGNETTE_FULL.R

# Some change in the code
# Another change

# clear all old variables from work environment
rm(list=ls())

# Required packages

# if running for the first time, you'll need a few packages specific to this workflow
# check to see if packages are installed first:
checkpkg <- function(pkg) {
  if (!(pkg %in% rownames(installed.packages()))) {
    install.packages(pkg)
  }
}

checkpkg("devtools")
checkpkg("REddyProc")
checkpkg("tidyverse")
checkpkg("lubridate")
checkpkg("patchwork")
checkpkg("lutz")
checkpkg("svDialogs")
checkpkg("shiny")
checkpkg("shinyTree")
checkpkg("mgcv")
checkpkg("ggforce")
checkpkg("gridExtra")
checkpkg("pracma")

library(REddyProc)
library(tidyverse) # bread 'n butta (ggplot2, tid1yr, dplyr, etc)
library(lubridate) # for easily manipulating date strings
library(patchwork) # creates composite figures with a clean interface
library(lutz) # allows you to determine UTC offset based on lat/long coords
library(svDialogs)
library(shiny)
library(shinyTree)
library(mgcv)
library(ggforce)
library(gridExtra)
library(pracma)




# # Use this if you want to run the current build of FluxSynthU
library(devtools)

if (!("FluxSynthU" %in% rownames(installed.packages()))) {
  install_github('ksmiff33/FluxSynthU', force=TRUE)
}

library(FluxSynthU)

# Access the site.meta dataset from package 'FluxSynthU'
site.meta <- FluxSynthU::site.meta



# FILE HANDLING -----------------------------------------------------------

## SET OUTPATH TO EXPORTED DATAFILES/FIGURES
# Specify the directory where you'd like to export all FluxSynth products.
# NOTE: this will create new subdirectories at the specified location.
FluxSynth_outpath <- ""





# Part 1. Process the raw data
# Specify the location where your raw flux data (FLUXNET2015, Ameriflux, and NEON) currently resides
raw_data_inpath <- "" # for FLUXNET, Ameriflux, and NEON

FS_fRawDataProcessing(inpath = raw_data_inpath, outpath = FluxSynth_outpath, site.meta = site.meta, flag = c(2:3), batch_process = FALSE)

rm(raw_data_inpath)




# Part 2. Perform gap-filling, partitioning with REddyProc on formatted raw data files (Created in Part 1)
inpath_REP <- paste0(FluxSynth_outpath, "/1_Processed data/1_REddyProc inputs/")
outpath_REP <- paste0(FluxSynth_outpath, "/1_Processed data/4_REddyProc outputs/")

FS_fProcessingThruREddyProc(method = 'both', inpath = inpath_REP, outpath = outpath_REP, site.meta = site.meta, batch_process = FALSE)

rm(inpath_REP, outpath_REP)




# Part 3. Append REddyProc output files (Created in Part 2)
inpath_REP_append <- paste0(FluxSynth_outpath, "/1_Processed data/4_REddyProc outputs/")
outpath_REP_append <- paste0(FluxSynth_outpath, "/1_Processed data/5_Stacked REddyProc outputs/")

FS_fAppendREddyProcOutputs(inpath = inpath_REP_append, outpath = outpath_REP_append, site.meta = site.meta)

rm(inpath_REP_append, outpath_REP_append)




# Part 4. Build complete output dataset from the following:
# 1. stacked 'night' and 'day' partitioned REddyProc output files (created in Part 3)
# 2. raw (half-)hourly data, including met variables (formatted using function 'FS_fRawDataProcessing') (Created in Part 1)
# 3. FLUXNET2015 data products (formatted using function 'FS_fRawDataProcessing') (if applicable) (Created in Part 1)

inpath_REP_stacked_output <- paste0(FluxSynth_outpath, "/1_Processed data/5_Stacked REddyProc outputs/")
inpath_raw_data <- paste0(FluxSynth_outpath, "/1_Processed data/3_Raw data for plotting/")
inpath_FLX_products <- paste0(FluxSynth_outpath, "/1_Processed data/2_FLUXNET2015 data products/")

outpath_complete_dataset <- paste0(FluxSynth_outpath, "/1_Processed data/6_Complete output datasets/")


FS_fBuildCompleteOutput(inpath_REP = inpath_REP_stacked_output,
                        inpath_raw = inpath_raw_data,
                        inpath_FLX = inpath_FLX_products,
                        outpath = outpath_complete_dataset, site.meta = site.meta, export.files = TRUE)

rm(inpath_REP_stacked_output, inpath_raw_data, inpath_FLX_products, outpath_complete_dataset)




# Part 5. Calculate GPPsat parameters from complete output datasets (created in Part 4)
inpath_complete_output_datasets <- paste0(FluxSynth_outpath, "/1_Processed data/6_Complete output datasets/")
outpath_GPPsat_parameters <- paste0(FluxSynth_outpath, "/1_Processed data/7_GPPsat data/")

FS_fCalculateGPPandNEESatParameters(window = 5, inpath = inpath_complete_output_datasets, outpath = outpath_GPPsat_parameters, site.meta, export.files = TRUE)

rm(inpath_complete_output_datasets, outpath_GPPsat_parameters)



# Part 6. Extract critical dates and make plots (using cumulative GPP to determine the GPP-free period, and GPPsat to determine the active period)
inpath_GPPsat <- paste0(FluxSynth_outpath, "/1_Processed data/7_GPPsat data/")
inpath_complete_output <- paste0(FluxSynth_outpath, "/1_Processed data/6_Complete output datasets/")

outpath_critdates <- paste0(FluxSynth_outpath, "/1_Processed data/8_Critical dates/")
outpath_plots <- paste0(FluxSynth_outpath, "/2_Documents/2_Critical dates/")

FS_fExtractAllCriticalDates(inpath_GPPsat, inpath_complete_output, outpath_critdates, outpath_plots, site.meta, span = 0.075, create.plots = FALSE)

rm(inpath_GPPsat, inpath_complete_output, outpath_critdates, outpath_plots)




# Part 7. Use winter critical dates to extract output data for winter only
inpath_fluxdat <- paste0(FluxSynth_outpath, "/1_Processed data/6_Complete output datasets/")
inpath_critdates <- paste0(FluxSynth_outpath, "/1_Processed data/8_Critical dates/") # Note: this folder may contain a subfolder of quality checked critical dates

outpath_winterfluxes <- paste0(FluxSynth_outpath, "/1_Processed data/9_Complete output datasets (winter only)/")

FS_fExtractWinterFluxes(inpath_fluxdat, inpath_critdates, outpath_winterfluxes, site.meta, qc = "auto")

rm(inpath_fluxdat, inpath_critdates, outpath_winterfluxes)




# Gonna make a change here
ksmiff33/FluxSynthU documentation built on Dec. 15, 2020, 10:29 p.m.